package com.rs.portal.doctor.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.web.servlet.ModelAndView;

import com.jaxplat.commons.Row;
import com.jaxplat.util.StringUtils;
import com.rs.commons.RsConstant;
import com.rs.commons.delegate.CommonDelegate;
import com.rs.commons.web.PortalBaseAction;
import com.rs.rsbiz.dept.delegate.SecCatalogDelegate;
import com.rs.rsbiz.doctor.delegate.DoctorDelegate;
import com.rs.rsbiz.hospital.delegate.HospitalDelegate;
import com.rs.util.Constants;
import com.rs.util.Pagination;

public class HospitalAction extends PortalBaseAction
{
    private DoctorDelegate doctorDelegate;

    private CommonDelegate commonDelegate;

    private SecCatalogDelegate secCatalogDelegate;

    private HospitalDelegate hospitalDelegate;

    public CommonDelegate getCommonDelegate()
    {
        return commonDelegate;
    }

    public void setCommonDelegate(CommonDelegate commonDelegate)
    {
        this.commonDelegate = commonDelegate;
    }

    public SecCatalogDelegate getSecCatalogDelegate()
    {
        return secCatalogDelegate;
    }

    public void setSecCatalogDelegate(SecCatalogDelegate secCatalogDelegate)
    {
        this.secCatalogDelegate = secCatalogDelegate;
    }

    public DoctorDelegate getDoctorDelegate()
    {
        return doctorDelegate;
    }

    public void setDoctorDelegate(DoctorDelegate doctorDelegate)
    {
        this.doctorDelegate = doctorDelegate;
    }

    public HospitalDelegate getHospitalDelegate()
    {
        return hospitalDelegate;
    }

    public void setHospitalDelegate(HospitalDelegate hospitalDelegate)
    {
        this.hospitalDelegate = hospitalDelegate;
    }

    public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);

        String cityCode = "";
        String currCityCode = RsConstant.DEFAULT_CITY_CODE;
        String currCityName = RsConstant.DEFAULT_CITY_NAME;
        if (StringUtils.isEmpty(request.getSession().getAttribute("city_code")))
        {
            cityCode = RsConstant.DEFAULT_CITY_CODE;
        }
        else
        {
            cityCode = (String) request.getSession().getAttribute("city_code");
        }

        Row cityInfo = this.commonDelegate.getCity(cityCode);
        if (null != cityInfo)
        {
            currCityCode = cityInfo.getString("city_code");
            currCityName = cityInfo.getString("city_name");
        }
        request.getSession().setAttribute("curr_city_code", currCityCode);
        request.getSession().setAttribute("curr_city_name", currCityName);

        params.put("city_code", cityCode);
        params.put("pageNumber", params.getInt("page") == 0 ? 1 : params.getInt("page"));
        params.put("pageSize", params.getInt("rows") == 0 ? 10 : params.getInt("rows"));
        params.put("pageIndex", params.getInt("pageNumber"));

        // params.put("pageSize", params.getInt("rows"));
        Object[] result = hospitalDelegate.getHosList(params);
        List rows = (List) result[0];
        Integer totalCount = (Integer) result[1];

        // 取得总记录数
        int pageIndex = 1;
        if (params.getInt("page") != 0)
        {
            pageIndex = params.getInt("page");
        }
        // 处理分页
        Pagination pagination = new Pagination();
        String pagingHtml = pagination.getGoogleToolsBar(totalCount.intValue(), "doPagination", pageIndex, RsConstant.PAGESIZE);
        Map attrs = new HashMap();
        attrs.put("totalCount", totalCount.intValue());
        attrs.put("rows", rows);
        attrs.put("pagingHtml", pagingHtml);
        attrs.put("request_uri", request.getRequestURI());
        return new ModelAndView("hos_list", attrs);
    }

    public ModelAndView viewHos(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        ModelAndView retView = new ModelAndView("redirect:/web2/hosAction.do?method=list");

        Row params = this.parseRequestParametersToRow(request);

        if (null != params.get("hospital_id"))
        {
            Row hosInfo = null;
            try
            {
                hosInfo = hospitalDelegate.getHosInfo(params);
            }
            catch (Exception e)
            {
                System.out.println(e.toString());
            }

            if (null != hosInfo)
            {
                Map attrs = new HashMap();
                
                Row catalogs = secCatalogDelegate.getAllCatalogOfHospital(params.getString("hospital_id"));
                List parentCatList = (List)catalogs.get("parent");
                List childCatList = (List)catalogs.get("child");
                attrs.put("hosInfo", hosInfo);
                attrs.put("parentCatList", parentCatList);
                attrs.put("childCatList", childCatList);
                attrs.put("request_uri", request.getRequestURI());
                return new ModelAndView("hos_info", attrs);
            }
            else
            {
                return new ModelAndView("redirect:/web2/hosAction.do?method=list");
            }
        }

        return retView;
    }
}